﻿//  --------------------------------
//  Copyright (c) AW Systems, Inc. All rights reserved.
//  This source code is made available under the terms of the Microsoft Public License (Ms-PL)
//  http://www.codeplex.com/SLAM/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Collections;
using Slam.Configuration;

namespace Slam.Data
{
	public static class DataContextExtender
	{
		/// <summary>
		/// Executes SQL commands directly on the database when commit is set to true, otherwise adds the SQL command to be executed
		/// to the SlamItemContext.ActivityLog
		/// </summary>
		/// <param name="context">DataContext for SQL server</param>
		/// <param name="commit">Indicates whether to directly execute given command</param>
		/// <param name="command">SQL command to be executed</param>
		/// <returns></returns>
		public static int ExecuteCommand(this DataContext context, bool commit, string command)
		{
			if (SlamItemContext.ActivityLog != null)
			{
				SlamItemContext.ActivityLog.WriteLine(command);
			}

			if (commit)
			{
				return context.ExecuteCommand(command);
			}
			else
			{
				return 1;
			}
		}

		/// <summary>
		/// Executes SQL queries directly on the database and returns objects of the given Type when commit is set to true, otherwise adds the SQL command to be executed
		/// to the SlamItemContext.ActivityLog
		/// </summary>
		/// <param name="context">DataContext for SQL server</param>
		/// <param name="commit">Indicates whether to directly execute given command</param>
		/// <param name="elementType">Type of element to return</param>
		/// <param name="command">SQL query to be executed</param>
		/// <returns></returns>
		public static IEnumerable ExecuteQuery(this DataContext context, bool commit, Type elementType, string command)
		{
			if (SlamItemContext.ActivityLog != null)
			{
				SlamItemContext.ActivityLog.WriteLine(command);
			}

			if (commit)
			{
				return context.ExecuteQuery(elementType, command);
			}
			else
			{
				return Array.CreateInstance(elementType, 0);
			}
		}

		/// <summary>
		/// Executes SQL queries directly on the database and returns objects when commit is set to true, otherwise adds the SQL command to be executed
		/// to the SlamItemContext.ActivityLog
		/// </summary>
		/// <param name="context">DataContext for SQL server</param>
		/// <param name="commit">Indicates whether to directly execute given command</param>
		/// <param name="command">SQL query to be executed</param>
		/// <returns></returns>
		public static IEnumerable<T> ExecuteQuery<T>(this DataContext context, bool commit, string command)
		{
			if (SlamItemContext.ActivityLog != null)
			{
				SlamItemContext.ActivityLog.WriteLine(command);
			}

			if (commit)
			{
				return context.ExecuteQuery<T>(command);
			}
			else
			{
				return new List<T>();
			}
		}
	}
}
